<template>
  <div class="setting-container">
    <!-- 卡片 -->
    <el-card>
      <!-- tabs -->

      <el-tabs v-model="activeName">
        <!-- 角色管理 -->
        <el-tab-pane label="角色管理" name="first">
          <!-- 老写法 -->
          <!-- <span slot="label"> 角色的管理</span> -->
          <!-- 2.6之后推荐的写法 -->
          <template v-slot:label>
            <span> 角色管理</span>
          </template>
          <!-- 按钮 -->
          <div class="body">
            <el-button
              class="addBtn"
              icon="el-icon-plus"
              type="primary"
              @click="showDialog"
            >新增角色</el-button>
            <!-- table -->
            <el-table
              v-loading="loading"
              border
              :data="tableData"
              style="width: 100%"
            >
              <el-table-column type="index" label="序号" width="180" />
              <el-table-column
                sortable
                prop="name"
                label="角色名"
                width="180"
              />
              <el-table-column sortable prop="description" label="描述" />
              <el-table-column width="300" label="操作">
                <template #default="{row}">
                  <el-button
                    type="text"
                    @click="showPerDialog(row.id)"
                  >分配权限</el-button>
                  <el-button
                    type="text"
                    @click="enterEdit(row.id)"
                  >修改</el-button>

                  <el-popconfirm
                    title="这是一段内容确定删除吗？"
                    @onConfirm="delRole(row.id)"
                  >
                    <el-button slot="reference" type="text">删除</el-button>
                  </el-popconfirm>
                </template>
              </el-table-column>
            </el-table>

            <!-- 分页 -->
            <el-pagination
              :current-page.sync="currentPage"
              :page-size.sync="pageSize"
              layout="total, prev, pager, next"
              :total="total"
              @current-change="handleCurrentChange"
            />
          </div>
        </el-tab-pane>
        <!-- 公司信息 -->
        <el-tab-pane label="公司信息" name="second">
          <template v-slot:label>
            <span> 公司信息</span>
          </template>
          <settingForm />
        </el-tab-pane>
      </el-tabs>
    </el-card>
    <!-- 弹框 -->
    <settingDialog ref="dialog" />
    <!-- 分配权限弹框 -->
    <perDialog ref="perDialog" />
  </div>
</template>

<script>
import settingForm from './components/settingForm.vue'
import { getRoles, getRoleById, delRoleById } from '../../api/setting'
import settingDialog from './components/settingDialog.vue'
import perDialog from './components/perDialog'
export default {
  name: 'Setting',
  components: {
    settingForm,
    settingDialog,
    perDialog
  },
  data() {
    return {
      currentPage: 1,
      // 页容量选项
      // 当前的页容量
      pageSize: 2,
      total: undefined,
      activeName: 'first',
      tableData: [],
      loading: false
    }
  },
  created() {
    this.getData()
  },
  methods: {
    // 分配权限
    showPerDialog(id) {
      this.$refs.perDialog.show(id)
    },
    // 删除
    async delRole(id) {
      await delRoleById(id)
      this.$message.success('删除成功')
      if (this.tableData.length === 1 && this.currentPage > 1) {
        this.currentPage--
      }
      this.getData()
    },
    // 进入修改
    async enterEdit(id) {
      const res = await getRoleById(id)
      this.$refs.dialog.form = res
      this.$refs.dialog.visible = true
    },
    // 显示弹框
    showDialog() {
      this.$refs.dialog.visible = true
    },
    // 获取数据
    async getData() {
      this.loading = true
      const res = await getRoles({
        page: this.currentPage,
        pagesize: this.pageSize
      })
      // console.log(res)
      this.tableData = res.rows
      this.total = res.total
      setTimeout(() => {
        this.loading = false
      }, 300)
    },

    // 页码改变
    // val 当前页码
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      this.getData()
    }
  }
}
</script>

<style lang="scss">
.setting-container {
  padding: 20px;
  .body {
    padding: 30px 50px;
    .addBtn {
      width: 100px;
      height: 40px;
      padding: 0;
      margin-bottom: 10px;
    }
  }
  .el-tabs__item {
    font-size: 20px;
    span {
      padding: 0 20px;
    }
  }
  .el-table {
    thead {
      th {
        background: #f0f2f5;
      }
    }
  }
  .el-pagination {
    text-align: right;
  }
}
</style>
